package com.liveneo.adapter.outinterface.util;

import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;

import com.google.gson.Gson;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Dom4JDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
  * @author liveneo-shuqw
  * @date 2016年8月9日 下午3:39:21
  * Copyright: Copyright (c) 2016 
  * Company: 北京朗泰
  * @version 3.0
  */
@SuppressWarnings({"rawtypes","unchecked"})
public final class XMLBeanUtils { 
    /** 
     * 将Bean转换为XML 
     * 
     * @param clazzMap 别名-类名映射Map 
     * @param bean     要转换为xml的bean对象 
     * @return XML字符串 
     */ 
    public static String bean2xml(Map<String, Class> clazzMap, Object bean) { 
        XStream xstream = new XStream(); 
        for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) { 
           
			Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next(); 
            xstream.alias(m.getKey(), m.getValue()); 
        } 
        String xml = xstream.toXML(bean).replace("__", "_"); 
        return xml; 
    } 

    /** 
     * 将XML转换为Bean 
     * 
     * @param clazzMap 别名-类名映射Map 
     * @param xml      要转换为bean对象的xml字符串 
     * @return Java Bean对象 
     */ 
    
	public static Object xml2Bean(Map<String, Class> clazzMap, String xml) { 
        XStream xstream = new XStream();
        for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) { 
            Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next(); 
            xstream.alias(m.getKey(), m.getValue()); 
        } 
        Object bean = xstream.fromXML(xml); 
        return bean; 
    } 

    /** 
     * 获取XStream对象 
     * 
     * @param clazzMap 别名-类名映射Map 
     * @return XStream对象 
     */ 
    public static XStream getXStreamObject(Map<String, Class> clazzMap) { 
        XStream xstream = new XStream(); 
        for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) { 
            Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next(); 
            xstream.alias(m.getKey(), m.getValue()); 
        } 
        return xstream; 
    } 
    
	/**
	 * xml 转化Json
	 * @param xml
	 * @param clazz
	 * @return
	 */
	public static String xmlToJson(InputStream in, Class<?> clazz) {
		XStream xstream = new XStream(new Dom4JDriver());
		xstream.setMode(XStream.NO_REFERENCES);
		xstream.alias("xml", clazz);
		return new Gson().toJson((xstream.fromXML(in)));
	}
    
	
	public static <T> T fromXML(String xml, Class<T> clazz){  
        XStream stream= new XStream(new DomDriver());  
        stream.processAnnotations(clazz); 
//        stream.ignoreUnknownElements();
        Object obj= stream.fromXML(xml);  
        try{  
            return clazz.cast(obj);  
        }catch(ClassCastException e){  
            return null;  
        }  
    }  
	
	/************
	 * 将实体对象转成对应对的xml文件
	 * @param obj 实体类
	 * @param encoding 编码方式
	 * @return
	 */
	public static String toXml(Object obj,String encoding){
		try {
			XStream xstream = new XStream(new DomDriver(encoding));
	        xstream.processAnnotations(obj.getClass()); // 识别obj类中的注解
	        // 以格式化的方式输出XML
	        return xstream.toXML(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }
	
  public static void main(String[] args) {
		// Map<String,Class> clazzMap = new HashMap<String,Class>();
	////	 clazzMap.put("com.estar.app.appsrvyycbx.domain.DataVO", DataVO.class);
	////	 String xml ="<com.estar.app.appsrvyycbx.domain.DataVO><resultCde>0</resultCde> <message>平台返回出错!</message><list/></com.estar.app.appsrvyycbx.domain.DataVO>";
	////	 System.out.println( xml2Bean(clazzMap, xml).toString());
	//	
	//	 String xml = "<data><message><status>2</status><value>识别成功</value></message><cardsinfo><card type='2'><item desc='保留'><![CDATA[]]></item><item desc='姓名'><![CDATA[匡文]]></item><item desc='性别'><![CDATA[男]]></item><item desc='民族'><![CDATA[汉]]></item><item desc='出生'><![CDATA[1988-11-28]]></item><item desc='住址'><![CDATA[长沙市天心区路边井44号]]></item><item desc='公民身份号码'><![CDATA[430103198811281514]]></item></card></cardsinfo></data>";
	//	 clazzMap.put("data", CardContentInfo.class);
	//	 clazzMap.put("cardsinfo", CardsInfo.class);
	//	// clazzMap.put("card", CardInfo.class);
	////	 clazzMap.put("items", List.class);  
	//	 clazzMap.put("item", Item.class); 
	//	// clazzMap.put("item", Item.class);
	//	 CardContentInfo card = (CardContentInfo)xml2Bean(clazzMap, xml);
	//	 System.out.println(card.getCardsinfo().getType());
  }
}
